package com.lantosec.hmrp.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.MonitorUtils;
import com.lantosec.hmrp.modules.monitor.entity.VisitEntity;
import com.lantosec.hmrp.modules.monitor.entity.VisitRecordDetailsEntity;
import com.lantosec.hmrp.modules.monitor.entity.VisitRecordEntity;
import com.lantosec.hmrp.modules.monitor.mapper.MonitorVisitMapper;
import com.lantosec.hmrp.modules.monitor.service.IMonitorVisitService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service()
@Primary
@Log4j2
@SuppressWarnings("all")
public class MonitorVisitServiceImpl extends ServiceImpl<MonitorVisitMapper, VisitEntity> implements IMonitorVisitService {
     
      /**
       * @date: 2020-06-28 10:41 
       * @instructions: 医生回访排名
       * @param: 
       * @return: 
       */
    @Override
    public List<VisitEntity> getVisitRank(String month, String year) {
        List<VisitEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] split = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.parseInt(split[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.parseInt(split[1]));
            param = baseMapper.getMonthData(startTime,endTime);
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
            param = baseMapper.getYearData(startTime);
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
            param = baseMapper.getMonthData(startTime,endTime);
        }
        return param;
    }

     
      /**
       * @date: 2020-06-28 10:41 
       * @instructions: 查看某个医生的回访记录
       * @param: userId 回访人id
       * @return: 
       */
    @Override
    public List<VisitRecordEntity> getVisitRecord(String month, String year, String userId) {
        List<VisitRecordEntity> param = null;
        String startTime = null;
        String endTime = null;
        if(StringUtils.isNotEmpty(month)){
            String[] split = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.parseInt(split[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.parseInt(split[1]));
            param = baseMapper.getVisitMonthRecord(startTime,endTime,userId);
        }else if(null != year){
            startTime = year;
            param = baseMapper.getVisitYearRecord(startTime,userId);
        }else{
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
            param = baseMapper.getVisitMonthRecord(startTime,endTime,userId);
        }
        return param;
    }

     
      /**
       * @date: 2020-06-28 10:54
       * @instructions: 查看回访详情
       * @param: revisitNoteId 回访记录id
       * @return: 
       */
    @Override
    public List<VisitRecordDetailsEntity> getVisitRecordDetails(String month, String year, String userId,String revisitTaskId) {
        String startTime = null;
        String endTime = null;
        List<VisitRecordDetailsEntity> param = null;
        if(StringUtils.isNotEmpty(month)){
            String[] split = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.parseInt(split[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.parseInt(split[1]));
            param = baseMapper.getVisitRecordMonthDetails(startTime,endTime,userId,revisitTaskId);
        }else if(null != year){
            startTime = year;
            param = baseMapper.getVisitYearDetails(startTime,userId,revisitTaskId);
        }else{
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
            param = baseMapper.getVisitRecordMonthDetails(startTime,endTime,userId,revisitTaskId);
        }
        return param;
    }
}
